package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.common.vo.dict.DictAreaVO;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.base.core.integration.DictIntegration;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.MatchingStatusEnum;
import com.jwsoft.manager.common.vo.eduAddressSchool.EduAddressSchoolKeyVO;
import com.jwsoft.manager.common.vo.eduAddressSchool.EduAddressSchoolQueryVO;
import com.jwsoft.manager.common.vo.eduAddressSchool.EduAddressSchoolSearchVO;
import com.jwsoft.manager.common.vo.eduAddressSchool.EduAddressSchoolVO;
import com.jwsoft.manager.common.vo.eduAddressSchoolAudit.EduAddressSchoolAuditQueryVO;
import com.jwsoft.manager.common.vo.eduAddressSchoolAudit.EduAddressSchoolAuditVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.core.dao.model.EduAddress;
import com.jwsoft.manager.core.dao.model.EduAddressSchool;
import com.jwsoft.manager.core.dao.model.EduAddressSchoolAudit;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.integration.EduAddressSchoolIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 地址学校关联表业务实现类
 *
 * @author wangtao
 * @since 2022-12-02
 */
@Slf4j
@ApiService(funcCode = "eduAddressSchool", title = "地址学校关联表")
public class EduAddressSchoolIntegrationImpl implements EduAddressSchoolIntegration {

    @Autowired
    private EduAddressSchoolService eduAddressSchoolService;
    @Autowired
    private EduAddressSchoolAuditService eduAddressSchoolAuditService;
    @Autowired
    private EduSchoolService eduSchoolService;
    @Autowired
    private EduAddressService eduAddressService;
    @Autowired
    private DictHelper dictHelper;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private CommonIntegration commonIntegration;
    @Autowired
    private EduAddressTreeSchoolService eduAddressTreeSchoolService;
    @Autowired
    private DictIntegration dictIntegration;

    @Override
    @OpApi(funcCode = "eduAddressSchool0001", title = "地址学校关联表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduAddressSchoolVO> getList(EduAddressSchoolQueryVO vo) {
        String schoolId = SessionUtil.getOrgId();
        //如果查询条件未选择统筹区，则将当前登陆用户的统筹区作为条件查询
        if (StringUtils.isBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        if(StringUtils.isNotBlank(vo.getAddressName())){
            vo.setAddressName(Convert.toDBC(vo.getAddressName()));
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<EduAddressSchool> addressSchoolQueryWrapper = new QueryWrapper<>();
        addressSchoolQueryWrapper
                .lambda()
                .eq(
                        !ObjectUtils.isEmpty(schoolId),
                        EduAddressSchool::getSchoolId,
                        schoolId)
                .eq(!ObjectUtils.isEmpty(vo.getNativeFlag()), EduAddressSchool::getNativeFlag, vo.getNativeFlag())
                .eq(!ObjectUtils.isEmpty(vo.getVerificationFlag()), EduAddressSchool::getVerificationFlag, vo.getVerificationFlag())
                .eq(!ObjectUtils.isEmpty(vo.getServiceAreaFlag()), EduAddressSchool::getServiceAreaFlag, vo.getServiceAreaFlag())
                .like(
                        !ObjectUtils.isEmpty(vo.getAddressName()),
                        EduAddressSchool::getAddressName,
                        vo.getAddressName())
                .orderByDesc(EduAddressSchool::getUpdateTime,EduAddressSchool::getAuthTime,EduAddressSchool::getAddressName);
        List<EduAddressSchool> list = eduAddressSchoolService.list(addressSchoolQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduAddressSchool> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduAddressSchoolVO> resultList = Convert.toList(EduAddressSchoolVO.class, pageInfo.getList());
        for (EduAddressSchoolVO eduAddressSchoolVO : resultList) {
            eduAddressSchoolVO.setAreaName(dictHelper.getAreaNameByCode(eduAddressSchoolVO.getAreaCode()));
            if(StringUtils.isBlank(eduAddressSchoolVO.getNativeFlag())){
                eduAddressSchoolVO.setNativeFlag(BoolEnum.FALSE.getType());
            }
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);

    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0002", title = "地址学校关联表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduAddressSchoolVO getById(EduAddressSchoolKeyVO vo) {
        EduAddressSchoolVO eduAddressSchoolVO = new EduAddressSchoolVO();
        if (StringUtils.isNotBlank(vo.getAddressSchoolId())) {
            EduAddressSchool addressSchool = eduAddressSchoolService.getById(vo.getAddressSchoolId());
            if (ObjectUtils.isEmpty(addressSchool)) {
                throw new AppException("查询不到详细信息!");
            }
            BeanUtil.copyProperties(addressSchool, eduAddressSchoolVO, CopyOptions.create().ignoreNullValue());
        } else if (!ObjectUtils.isEmpty(vo.getId())) {
            EduAddressSchoolAudit addressSchool = eduAddressSchoolAuditService.getById(vo.getId());
            if (ObjectUtils.isEmpty(addressSchool)) {
                throw new AppException("查询不到详细信息!");
            }
            BeanUtil.copyProperties(addressSchool, eduAddressSchoolVO, CopyOptions.create().ignoreNullValue());
        } else {
            throw new AppException("查询的主键不能为空");
        }
        return eduAddressSchoolVO;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "eduAddressSchool0003", title = "地址学校关联表保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduAddressSchoolVO vo) {
        //公共校验
        String schoolId = SessionUtil.getOrgId();
        if (StringUtils.isBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        if (!schoolId.equals(vo.getSchoolId())) {
            throw new AppException("当前账号无绑定该学校的权限");
        }
        EduSchoolVO schoolVO = eduSchoolService.getDetail(schoolId);
        if (schoolVO == null) {
            throw new AppException("当前账号绑定组织不是学校");
        }
        vo.setSchoolCategory(schoolVO.getSchoolCategory());
        //主键为空为新增校验
        if (StringUtils.isBlank(vo.getAddressSchoolId())) {
            //校验关联是否已存在
            QueryWrapper<EduAddressSchool> countQueryWrapper = new QueryWrapper<>();
            countQueryWrapper.lambda().eq(EduAddressSchool::getAddressId, vo.getAddressId())
                    .eq(EduAddressSchool::getSchoolId, schoolId);
            if (eduAddressSchoolService.count(countQueryWrapper) > 0) {
                throw new AppException("该地址已关联");
            }
            EduAddress eduAddress;
            if (StringUtils.isBlank(vo.getAddressId())) {
                if (StringUtils.isBlank(vo.getAddressName())) {
                    throw new AppException("地址不能为空");
                }
                QueryWrapper<EduAddress> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(EduAddress::getAddressName, vo.getAddressName());
                List<EduAddress> list = eduAddressService.list(queryWrapper);
                if (CollectionUtils.isEmpty(list)) {
                    throw new AppException("地址库中不存在该地址");
                }
                if (list.size() > 1) {
                    //TODO 多条地址怎么处理
                    throw new AppException("地址库中存在多条地址");
                }
                eduAddress = list.get(0);
            } else {
                eduAddress = eduAddressService.getById(vo.getAddressId());
                vo.setAddressName(eduAddress.getAddressName());
            }
            vo.setAddressId(eduAddress.getAddressId());
            vo.setTreeId(eduAddress.getAddressTreeId());
            if (eduAddress.getAddressTreeId() != null) {
                vo.setTreeId(eduAddress.getAddressTreeId());
            }
        }
        //主键不为空为修改校验
        else {
            EduAddressSchool oldEntity = eduAddressSchoolService.getById(vo.getAddressSchoolId());
            if (ObjectUtils.isEmpty(oldEntity)) {
                throw new AppException("查询不到需要修改的信息!");
            }
        }
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0004", title = "地址学校关联表新增", funcType = FuncTypeEnum.insert)
    @Transactional(rollbackFor = Exception.class)
    public EduAddressSchoolVO add(EduAddressSchoolVO vo) {
        //保存前校验
        checkSave(vo);
        //判断学校是否有审核数据，有的话通过审核新增，不能直接关联
        LambdaQueryWrapper<EduAddressSchoolAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduAddressSchoolAudit::getSchoolId, vo.getSchoolId())
                .eq(EduAddressSchoolAudit::getAuthStatus, AuthStatusEnum.NODO.getType())
                .eq(EduAddressSchoolAudit::getAddressId, vo.getAddressId());
        if (eduAddressSchoolAuditService.count(queryWrapper) > 0) {
            throw new AppException("存在未审核数据，请先去待审核地址页面进行查看并审核");
        }
        EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
        auditVO.setId(null);
        auditVO.setAddressId(vo.getAddressId());
        auditVO.setSchoolId(vo.getSchoolId());
        if(StringUtils.isBlank(vo.getNativeFlag())){
            auditVO.setNativeFlag(BoolEnum.FALSE.getType());
        }
        if(StringUtils.isBlank(vo.getServiceAreaFlag())){
            auditVO.setServiceAreaFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getVerificationFlag())) {
            auditVO.setVerificationFlag(BoolEnum.FALSE.getType());
        }
        if (BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getNativeFlag())){
            auditVO.setVerificationFlag(BoolEnum.TRUE.getType());
        }
        auditVO.setNativeFlag(vo.getNativeFlag());
        auditVO.setServiceAreaFlag(vo.getServiceAreaFlag());
        //保存数据
        String addressSchoolId = eduAddressSchoolService.boundSchoolAndAddress(auditVO);
        //将主键设置回vo使操作日志可以取到
        vo.setAddressSchoolId(addressSchoolId);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0011", title = "地址学校关联表新增批量", funcType = FuncTypeEnum.insert)
    @Transactional(rollbackFor = Exception.class)
    public void addBatch(EduAddressSchoolVO vo) {
        if (CollectionUtils.isEmpty(vo.getAddressIdList())) {
            throw new AppException("传入的关联地址信息不能为空");
        }
        if (StringUtils.isBlank(vo.getSchoolId())){
            vo.setSchoolId(SessionUtil.getOrgId());
        }
        EduSchoolVO detail = eduSchoolService.getDetail(vo.getSchoolId());
        if (detail == null){
            throw new AppException("学校信息不存在,请登录学校账号进行处理！");
        }
        for (String addressId : vo.getAddressIdList()) {
            EduAddressSchoolVO eduAddressSchoolVO = new EduAddressSchoolVO();
            BeanUtil.copyProperties(vo, eduAddressSchoolVO);
            eduAddressSchoolVO.setAddressId(addressId);
            this.add(eduAddressSchoolVO);
        }
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0005", title = "地址学校关联表修改", funcType = FuncTypeEnum.update)
    @Transactional(rollbackFor = Exception.class)
    public EduAddressSchoolVO edit(EduAddressSchoolVO vo) {
        if (StringUtils.isBlank(vo.getAddressSchoolId())) {
            throw new AppException("地址学校关联id不能为空");
        }
        //保存前校验
        checkSave(vo);
        if(StringUtils.isBlank(vo.getNativeFlag())){
            vo.setNativeFlag(BoolEnum.FALSE.getType());
        }
        if(StringUtils.isBlank(vo.getServiceAreaFlag())){
            vo.setServiceAreaFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getVerificationFlag())) {
            vo.setVerificationFlag(BoolEnum.FALSE.getType());
        }

        EduAddressSchool entity = Convert.convert(EduAddressSchool.class, vo);
        eduAddressSchoolService.updateById(entity);
        //处理原来审核通过数据,更新是否原住民可读
        LambdaUpdateWrapper<EduAddressSchoolAudit> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(EduAddressSchoolAudit::getSchoolId,entity.getSchoolId())
                .eq(EduAddressSchoolAudit::getAddressId,entity.getAddressId())
                .eq(EduAddressSchoolAudit::getAuthStatus,AuthStatusEnum.YES.getType())
                .set(EduAddressSchoolAudit::getNativeFlag,entity.getNativeFlag())
                .set(EduAddressSchoolAudit::getServiceAreaFlag,entity.getServiceAreaFlag())
                .set(EduAddressSchoolAudit::getVerificationFlag,entity.getVerificationFlag());
        eduAddressSchoolAuditService.update(updateWrapper);
        eduAddressSchoolService.renewalBeforeFlagByAddressId(entity.getAddressId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0006", title = "地址学校关联表根据主键删除", funcType = FuncTypeEnum.delete)
    @Transactional(rollbackFor = Exception.class)
    public void del(EduAddressSchoolKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getAddressSchoolId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduAddressSchool entity = eduAddressSchoolService.getById(vo.getAddressSchoolId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduAddressSchoolService.unboundSchoolAndAddress(entity.getAddressId(), entity.getSchoolId());
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0026", title = "学校地址预警取消关联", funcType = FuncTypeEnum.delete,checkPrivilege = BoolEnum.FALSE)
    @Transactional(rollbackFor = Exception.class)
    public void delByErrors(EduAddressSchoolKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getAddressName())){
            throw new AppException("地址名称不能为空");
        }
        String schoolId = SessionUtil.getOrgId();
        if (StringUtils.isBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        EduSchoolVO school = eduSchoolService.getDetail(schoolId);
        if (ObjectUtils.isEmpty(school)){
            throw new AppException("该学校不存在，数据异常！");
        }
        //根据地址名称和学校查询
        QueryWrapper<EduAddressSchool> eduAddressSchoolQueryWrapper = new QueryWrapper<>();
        eduAddressSchoolQueryWrapper.lambda()
                .eq(EduAddressSchool::getSchoolId,schoolId)
                .eq(EduAddressSchool::getAddressName,vo.getAddressName());
        List<EduAddressSchool> list = eduAddressSchoolService.list(eduAddressSchoolQueryWrapper);
        if (CollectionUtils.isEmpty(list)){
            throw new AppException("未找到需要删除的信息!");
        }
        if (list.size() > 1){
            throw new AppException("数据异常，请联系管理员！");
        }
        vo.setAddressSchoolId(list.get(0).getAddressSchoolId());
        this.del(vo);
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0010", title = "地址学校关联表根据主键批量删除", funcType = FuncTypeEnum.delete)
    @Transactional(rollbackFor = Exception.class)
    public void delBatch(EduAddressSchoolKeyVO vo) {
        if (CollectionUtils.isEmpty(vo.getAddressSchoolIdList())) {
            throw new AppException("未传入需要删除的信息");
        }
        for (String addressSchoolId : vo.getAddressSchoolIdList()) {
            EduAddressSchoolKeyVO keyVO = new EduAddressSchoolKeyVO();
            keyVO.setAddressSchoolId(addressSchoolId);
            this.del(keyVO);
        }
    }

    @OpApi(funcCode = "eduAddressSchool0007", title = "查询待审核信息", funcType = FuncTypeEnum.query)
    @Override
    public PageInfo<EduAddressSchoolAuditVO> queryAddressSchoolAudit(EduAddressSchoolAuditQueryVO vo) {
        String schoolId = SessionUtil.getOrgId();
        if (StringUtils.isBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        EduSchoolVO school = eduSchoolService.getDetail(schoolId);
        if (ObjectUtils.isEmpty(school)){
            throw new AppException("该学校不存在，数据异常！");
        }
        CommonVO addressParseCheck = new CommonVO();
        addressParseCheck.setParamKey("address_audit_bn_flag");
        addressParseCheck.setAreaCode(vo.getAreaCode());
        String auditBnFlag = commonIntegration.getValueByKey(addressParseCheck);
        //设置分页信息
        if (vo.getPageNum() == null) {
            vo.setPageNum(1);
        }
        if (vo.getPageSize() == null || vo.getPageSize() == 0) {
            vo.setPageNum(20);
        }
        if (StringUtils.isEmpty(auditBnFlag)){
            //如果为空或者没有配置，默认查询本年的数据
            auditBnFlag = BoolEnum.TRUE.getType();
        }
        List<EduAddressSchoolAudit> list = new ArrayList<>();
        if (BoolEnum.TRUE.getType().equals(auditBnFlag)){
            PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            vo.setSchoolCategory(school.getSchoolCategory());
            vo.setSchoolId(school.getOrgId());
            list =  eduAddressSchoolAuditService.queryAuditAddress(vo);
        }else{
            QueryWrapper<EduAddressSchoolAudit> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(EduAddressSchoolAudit::getSchoolId, schoolId);
            if (StringUtils.isNotBlank(vo.getAuthStatus())) {
                queryWrapper.lambda().eq(EduAddressSchoolAudit::getAuthStatus, vo.getAuthStatus());
            }
            if (StringUtils.isNotBlank(vo.getAddressName())) {
                queryWrapper.lambda().like(EduAddressSchoolAudit::getAddressName, vo.getAddressName());
            }
            queryWrapper.lambda().orderByDesc(EduAddressSchoolAudit::getAuthTime,EduAddressSchoolAudit::getUpdateTime);
            PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            list = eduAddressSchoolAuditService.list(queryWrapper);
        }
        if (CollectionUtils.isEmpty(list)) {
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        PageInfo<EduAddressSchoolAudit> modelPageInfo = new PageInfo<>(list);
        List<EduAddressSchoolAuditVO> voList = Convert.toList(EduAddressSchoolAuditVO.class, modelPageInfo.getList());
        for (EduAddressSchoolAuditVO eduAddressSchoolAuditVO : voList) {
            eduAddressSchoolAuditVO.setAreaName(dictHelper.getAreaNameByCode(eduAddressSchoolAuditVO.getAreaCode()));
        }
        return PagerUtil.parsePagerVo(voList, modelPageInfo);
    }

    @OpApi(funcCode = "eduAddressSchool0008", title = "地址学校关联审核", funcType = FuncTypeEnum.auth)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doAudit(EduAddressSchoolAuditVO auditVO) {
        String schoolId = SessionUtil.getOrgId();
        if (!StringUtils.isNotBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        if (!AuthStatusEnum.YES.getType().equals(auditVO.getAuthStatus())
                && !AuthStatusEnum.NO.getType().equals(auditVO.getAuthStatus())) {
            throw new AppException("审核状态不正确");
        }
        EduAddressSchoolAudit addressSchoolAudit = eduAddressSchoolAuditService.getById(auditVO.getId());
        if (addressSchoolAudit == null) {
            throw new AppException("未查询到审核数据信息");
        }
        if (!AuthStatusEnum.NODO.getType().equals(addressSchoolAudit.getAuthStatus())) {
            throw new AppException("当前数据不是待审核状态");
        }
        if (!schoolId.equals(addressSchoolAudit.getSchoolId())) {
            throw new AppException("当前账号所属学校和待审数据学校不匹配");
        }

        //学校信息
        EduSchoolVO schoolDetail = eduSchoolService.getDetail(schoolId);
        //设置审核人、审核时间
        auditVO.setAuthUserId(SessionUtil.getUserId());
        auditVO.setAuthTime(new Date());
        if (StringUtils.isBlank(auditVO.getNativeFlag())) {
            auditVO.setNativeFlag(BoolEnum.FALSE.getType());
        }
        addressSchoolAudit.setNativeFlag(auditVO.getNativeFlag());
        addressSchoolAudit.setAuthStatus(auditVO.getAuthStatus());
        addressSchoolAudit.setAuthRemarks(auditVO.getAuthRemarks());
        addressSchoolAudit.setAuthUserId(SessionUtil.getUserId());
        addressSchoolAudit.setAuthTime(new Date());
        addressSchoolAudit.setNativeFlag(auditVO.getNativeFlag());
        addressSchoolAudit.setSchoolCategory(schoolDetail.getSchoolCategory());
        //修改审核信息
        UpdateWrapper<EduAddressSchoolAudit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(EduAddressSchoolAudit::getId, auditVO.getId())
                .eq(EduAddressSchoolAudit::getAuthStatus, AuthStatusEnum.NODO.getType())
                .set(EduAddressSchoolAudit::getAuthStatus, auditVO.getAuthStatus())
                .set(EduAddressSchoolAudit::getNativeFlag, auditVO.getNativeFlag())
                .set(EduAddressSchoolAudit::getAuthRemarks, auditVO.getAuthRemarks())
                .set(EduAddressSchoolAudit::getAuthTime, auditVO.getAuthTime())
                .set(EduAddressSchoolAudit::getAuthUserId, auditVO.getAuthUserId())
                .set(EduAddressSchoolAudit::getNativeFlag, auditVO.getNativeFlag());
        if (!eduAddressSchoolAuditService.update(updateWrapper)) {
            throw new AppException("审核状态更新失败,请刷新待审列表后重试");
        }
        if (AuthStatusEnum.YES.getType().equals(auditVO.getAuthStatus())) {
            //绑定
            eduAddressSchoolService.bound(addressSchoolAudit.getAddressId(), addressSchoolAudit.getSchoolId());
        }
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0012", title = "地址学校关联批量审核", funcType = FuncTypeEnum.auth)
    @Transactional
    public void doAuditBatch(EduAddressSchoolAuditVO auditVO) {
        if (StringUtils.isBlank(auditVO.getSchoolId())){
            auditVO.setSchoolId(SessionUtil.getOrgId());
        }
        EduSchoolVO detail = eduSchoolService.getDetail(auditVO.getSchoolId());
        if (detail == null){
            throw new AppException("学校信息不存在,请登录学校账号进行处理！");
        }
        if (CollectionUtils.isEmpty(auditVO.getIdList())) {
            throw new AppException("需要审核id列表不能为空");
        }
        if (StringUtils.isBlank(auditVO.getAuthStatus())) {
            throw new AppException("审核状态不能为空");
        }
        if (StringUtils.isBlank(auditVO.getNativeFlag())) {
            auditVO.setNativeFlag(BoolEnum.FALSE.getType());
        }
        for (Long id : auditVO.getIdList()) {
            EduAddressSchoolAuditVO vo = new EduAddressSchoolAuditVO();
            BeanUtil.copyProperties(auditVO, vo);
            vo.setId(id);
            this.addressAudit(vo);
        }
    }


    @Override
    @OpApi(funcCode = "eduAddressSchool0009", title = "关联地址信息查询", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduAddressSchoolVO> queryAddress(EduAddressSchoolSearchVO vo) {
        if (StringUtils.isBlank(vo.getContactType())) {
            throw new AppException("请先选择是否关联标识");
        }
        if (StringUtils.isBlank(vo.getSchoolId())) {
            vo.setSchoolId(SessionUtil.getOrgId());
        }
        if (StringUtils.isNotBlank(vo.getAddressName())){
            vo.setAddressName(Convert.toDBC(vo.getAddressName()));
        }
        EduSchoolVO eduSchoolVO=eduSchoolService.getDetail(vo.getSchoolId());
        if(eduSchoolVO==null){
            throw new AppException("您的账号不是学校账号，不能操作此功能");
        }
        //获取账号行政区划以及本身行政区划  金华下四个区以及金华市
        List<String> areaCodeList = new ArrayList<>();
        String areaCode = eduHelper.thisAreaCode();
        String sessionAreaCode = SessionUtil.getAreaCode();
        if (ObjectUtils.isEmpty(vo.getAreaCode())){
            DictAreaVO dictAreaVO = new DictAreaVO();
            dictAreaVO.setParentId(eduHelper.thisAreaCode());
            dictAreaVO.setPermissionFlag(false);
            List<SelectVO> selectVOList = dictIntegration.getDistrictByOrg(dictAreaVO);
            areaCodeList = selectVOList.stream()
                    .map(SelectVO::getDictKey)
                    .collect(Collectors.toList());
            if (!areaCodeList.contains(sessionAreaCode)) {
                areaCodeList.add(sessionAreaCode);
            }
        }
        //如果当前行政区划没在330701，330702，330703，330704则查询登录账号的行政区划地址
        List<String> setCodeList = new ArrayList<>();
        Set<String> jhAreaCodeSet = new HashSet<>(Arrays.asList("330701", "330702", "330703", "330704"));
        if (!jhAreaCodeSet.contains(sessionAreaCode)) {
            setCodeList.add(sessionAreaCode);
            setCodeList.add(areaCode);
        } else {
            setCodeList.addAll(jhAreaCodeSet);
            setCodeList.add(areaCode);
        }
        setCodeList.add(areaCode);
        vo.setAreaCodeList(setCodeList);
        vo.setSchoolCategory(eduSchoolVO.getSchoolCategory());
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(50);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        vo.setSchoolCategory(eduSchoolVO.getSchoolCategory());
        List<EduAddressSchoolVO> list = eduAddressSchoolService.getAddressByNoGl(vo);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        return new PageInfo<>(list);
    }

    @OpApi(funcCode = "eduAddressSchool0008", title = "地址学校关联审核", funcType = FuncTypeEnum.auth)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addressAudit(EduAddressSchoolAuditVO vo) {
        String schoolId = SessionUtil.getOrgId();
        if (!StringUtils.isNotBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        if (!AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())
                && !AuthStatusEnum.NO.getType().equals(vo.getAuthStatus())) {
            throw new AppException("审核状态不正确");
        }
        EduAddressSchoolAudit addressSchoolAudit = eduAddressSchoolAuditService.getById(vo.getId());
        if (addressSchoolAudit == null) {
            throw new AppException("未查询到审核数据信息");
        }
        if (!AuthStatusEnum.NODO.getType().equals(addressSchoolAudit.getAuthStatus())) {
            throw new AppException("当前数据不是待审核状态");
        }
        if (!schoolId.equals(addressSchoolAudit.getSchoolId())) {
            throw new AppException("当前账号所属学校和待审数据学校不匹配");
        }
        EduAddress eduAddress = eduAddressService.getById(addressSchoolAudit.getAddressId());
        if (ObjectUtils.isEmpty(eduAddress)){
            throw new AppException("地址信息有误，请检查数据是否正常！");
        }
        //学校信息
        EduSchoolVO schoolDetail = eduSchoolService.getDetail(schoolId);
        //设置审核人、审核时间
        vo.setAuthUserId(SessionUtil.getUserId());
        vo.setAuthTime(new Date());
        if (StringUtils.isBlank(vo.getNativeFlag())) {
            vo.setNativeFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getServiceAreaFlag())) {
            vo.setServiceAreaFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getVerificationFlag())) {
            vo.setVerificationFlag(BoolEnum.FALSE.getType());
        }
        if(BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getNativeFlag())){
            vo.setVerificationFlag(BoolEnum.TRUE.getType());
        }

        addressSchoolAudit.setNativeFlag(vo.getNativeFlag());
        addressSchoolAudit.setServiceAreaFlag(vo.getServiceAreaFlag());
        addressSchoolAudit.setAuthStatus(vo.getAuthStatus());
        addressSchoolAudit.setAuthRemarks(vo.getAuthRemarks());
        addressSchoolAudit.setAuthUserId(SessionUtil.getUserId());
        addressSchoolAudit.setAuthTime(new Date());
        addressSchoolAudit.setVerificationFlag(vo.getVerificationFlag());
        addressSchoolAudit.setSchoolCategory(schoolDetail.getSchoolCategory());
        //修改审核信息
        if (!eduAddressSchoolAuditService.updateById(addressSchoolAudit)) {
            throw new AppException("审核状态更新失败,请刷新待审列表后重试");
        }
        //如果审核状态通过，将数据同步到edu_address_tree表中
        if (AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())) {
            EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
            auditVO.setAddressId(addressSchoolAudit.getAddressId());
            auditVO.setSchoolId(addressSchoolAudit.getSchoolId());
            auditVO.setTreeId(eduAddress.getAddressTreeId());
            auditVO.setId(addressSchoolAudit.getId());
            auditVO.setNativeFlag(addressSchoolAudit.getNativeFlag());
            auditVO.setServiceAreaFlag(addressSchoolAudit.getServiceAreaFlag());
            auditVO.setVerificationFlag(addressSchoolAudit.getVerificationFlag());
            eduAddressSchoolService.boundSchoolAndAddress(auditVO);
        }else{
            EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
            auditVO.setSchoolId(addressSchoolAudit.getSchoolId());
            auditVO.setAddressId(addressSchoolAudit.getAddressId());
            auditVO.setTreeId(eduAddress.getAddressTreeId());
            //1.审核不通过，删除地址叶子节点关联学校  2.判断上级节点是否能删。
            eduAddressTreeSchoolService.removeAddressTreeNodeRelationSchool(auditVO);
        }
        //校验地址学区匹配的状态
        eduAddressService.checkAddressMatchingStatus(eduAddress);
    }

    public void addressAuditAuto(EduAddressSchoolAuditVO vo) {
        if (!StringUtils.isNotBlank(vo.getSchoolId())) {
            throw new AppException("没有学校信息");
        }
        if (!AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())
                && !AuthStatusEnum.NO.getType().equals(vo.getAuthStatus())) {
            throw new AppException("审核状态不正确");
        }
        EduAddressSchoolAudit addressSchoolAudit = eduAddressSchoolAuditService.getById(vo.getId());
        if (addressSchoolAudit == null) {
            throw new AppException("未查询到审核数据信息");
        }
        if (!AuthStatusEnum.NODO.getType().equals(addressSchoolAudit.getAuthStatus())) {
            throw new AppException("当前数据不是待审核状态");
        }
        EduAddress eduAddress = eduAddressService.getById(addressSchoolAudit.getAddressId());
        if (ObjectUtils.isEmpty(eduAddress)){
            throw new AppException("地址信息有误，请检查数据是否正常！");
        }
        //学校信息
        EduSchoolVO schoolDetail = eduSchoolService.getDetail(vo.getSchoolId());
        //设置审核人、审核时间
        vo.setAuthUserId(SessionUtil.getUserId());
        vo.setAuthTime(new Date());
        if (StringUtils.isBlank(vo.getNativeFlag())) {
            vo.setNativeFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getServiceAreaFlag())) {
            vo.setServiceAreaFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getVerificationFlag())) {
            vo.setVerificationFlag(BoolEnum.FALSE.getType());
        }
        if(BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getNativeFlag())){
            vo.setVerificationFlag(BoolEnum.TRUE.getType());
        }

        addressSchoolAudit.setNativeFlag(vo.getNativeFlag());
        addressSchoolAudit.setServiceAreaFlag(vo.getServiceAreaFlag());
        addressSchoolAudit.setAuthStatus(vo.getAuthStatus());
        addressSchoolAudit.setAuthRemarks(vo.getAuthRemarks());
        addressSchoolAudit.setAuthUserId(SessionUtil.getUserId());
        addressSchoolAudit.setAuthTime(new Date());
        addressSchoolAudit.setVerificationFlag(vo.getVerificationFlag());
        addressSchoolAudit.setSchoolCategory(schoolDetail.getSchoolCategory());
        //修改审核信息
        if (!eduAddressSchoolAuditService.updateById(addressSchoolAudit)) {
            throw new AppException("审核状态更新失败,请刷新待审列表后重试");
        }
        //如果审核状态通过，将数据同步到edu_address_tree表中
        if (AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())) {
            EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
            auditVO.setAddressId(addressSchoolAudit.getAddressId());
            auditVO.setSchoolId(addressSchoolAudit.getSchoolId());
            auditVO.setTreeId(eduAddress.getAddressTreeId());
            auditVO.setId(addressSchoolAudit.getId());
            auditVO.setNativeFlag(addressSchoolAudit.getNativeFlag());
            auditVO.setServiceAreaFlag(addressSchoolAudit.getServiceAreaFlag());
            auditVO.setVerificationFlag(addressSchoolAudit.getVerificationFlag());
            auditVO.setAuditAuto("1");//自动审核标志，1是0否
            eduAddressSchoolService.boundSchoolAndAddress(auditVO);
        }else{
            EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
            auditVO.setSchoolId(addressSchoolAudit.getSchoolId());
            auditVO.setAddressId(addressSchoolAudit.getAddressId());
            auditVO.setTreeId(eduAddress.getAddressTreeId());
            //1.审核不通过，删除地址叶子节点关联学校  2.判断上级节点是否能删。
            eduAddressTreeSchoolService.removeAddressTreeNodeRelationSchool(auditVO);
        }
        //校验地址学区匹配的状态
        eduAddressService.checkAddressMatchingStatus(eduAddress);
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0013", title = "学校本年度地址库查询", funcType = FuncTypeEnum.auth)
    public PageInfo<EduAddressSchoolVO> querySchoolAddressByNowYear(EduAddressSchoolQueryVO vo) {
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        if (StringUtils.isBlank(vo.getSchoolId())) {
            vo.setSchoolId(SessionUtil.getOrgId());
        }
        EduSchoolVO eduSchoolVO=eduSchoolService.getDetail(vo.getSchoolId());
        if(eduSchoolVO==null){
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        vo.setSchoolCategory(eduSchoolVO.getSchoolCategory());
        vo.setAreaCode(eduSchoolVO.getAreaCode());
        if (ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<EduAddressSchool> eduAddressSchools = eduAddressSchoolService.querySchoolAddressByNowYear(vo);
        if (ObjectUtils.isEmpty(eduAddressSchools)){
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        PageInfo<EduAddressSchool> pageInfo = new PageInfo<>(eduAddressSchools);
        List<EduAddressSchoolVO> resultList = Convert.toList(EduAddressSchoolVO.class, pageInfo.getList());
        // 不为空则获取分页结果
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        for (EduAddressSchoolVO eduAddressSchoolVO : resultList) {
            eduAddressSchoolVO.setAreaName(dictHelper.getAreaNameByCode(eduAddressSchoolVO.getAreaCode()));
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0014", title = "本年全市未关联地址查询", funcType = FuncTypeEnum.other,checkPrivilege = BoolEnum.FALSE)
    @Transactional
    public PageInfo<EduAddressSchoolVO> nowYearNoRelationAddress(EduAddressSchoolSearchVO vo) {
        return this.queryAddress(vo);
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0015", title = "查询本年审核不通过信息", funcType = FuncTypeEnum.query ,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduAddressSchoolAuditVO> queryNoPassAuditAddressSchoolAudit(EduAddressSchoolAuditQueryVO vo) {
        String schoolId = SessionUtil.getOrgId();
        if (StringUtils.isBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        EduSchoolVO school = eduSchoolService.getDetail(schoolId);
        if (ObjectUtils.isEmpty(school)){
            throw new AppException("该学校不存在，数据异常！");
        }
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        List<EduAddressSchoolAudit> list = new ArrayList<>();
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        vo.setSchoolId(school.getOrgId());
        list =  eduAddressSchoolAuditService.queryNoPassAuditAddress(vo);
        if (CollectionUtils.isEmpty(list)) {
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        PageInfo<EduAddressSchoolAudit> modelPageInfo = new PageInfo<>(list);
        List<EduAddressSchoolAuditVO> voList = Convert.toList(EduAddressSchoolAuditVO.class, modelPageInfo.getList());
        for (EduAddressSchoolAuditVO eduAddressSchoolAuditVO : voList) {
            eduAddressSchoolAuditVO.setAreaName(dictHelper.getAreaNameByCode(eduAddressSchoolAuditVO.getAreaCode()));
        }
        return PagerUtil.parsePagerVo(voList, modelPageInfo);
    }

    @OpApi(funcCode = "eduAddressSchool0016", title = "审核失败地址退回审核通过", funcType = FuncTypeEnum.auth)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void noPassAddressAudit(EduAddressSchoolAuditVO vo) {
        String schoolId = SessionUtil.getOrgId();
        if (!StringUtils.isNotBlank(schoolId)) {
            throw new AppException("当前账号未绑定学校");
        }
        if (!AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())
                && !AuthStatusEnum.NO.getType().equals(vo.getAuthStatus())) {
            throw new AppException("审核状态不正确");
        }
        EduAddressSchoolAudit addressSchoolAudit = eduAddressSchoolAuditService.getById(vo.getId());
        if (addressSchoolAudit == null) {
            throw new AppException("未查询到审核数据信息");
        }
        if (!AuthStatusEnum.NO.getType().equals(addressSchoolAudit.getAuthStatus())) {
            throw new AppException("当前数据不是审核不通过数据");
        }
        if (!schoolId.equals(addressSchoolAudit.getSchoolId())) {
            throw new AppException("当前账号所属学校和待审数据学校不匹配");
        }
        EduAddress eduAddress = eduAddressService.getById(addressSchoolAudit.getAddressId());
        if (ObjectUtils.isEmpty(eduAddress)){
            throw new AppException("地址信息有误，请检查数据是否正常！");
        }
        //学校信息
        EduSchoolVO schoolDetail = eduSchoolService.getDetail(schoolId);
        //设置审核人、审核时间
        vo.setAuthUserId(SessionUtil.getUserId());
        vo.setAuthTime(new Date());
        if (StringUtils.isBlank(vo.getNativeFlag())) {
            vo.setNativeFlag(BoolEnum.FALSE.getType());
        }
        addressSchoolAudit.setNativeFlag(vo.getNativeFlag());
        addressSchoolAudit.setServiceAreaFlag(vo.getServiceAreaFlag());
        addressSchoolAudit.setAuthStatus(vo.getAuthStatus());
        addressSchoolAudit.setAuthRemarks(vo.getAuthRemarks());
        addressSchoolAudit.setAuthUserId(SessionUtil.getUserId());
        addressSchoolAudit.setAuthTime(new Date());
        addressSchoolAudit.setSchoolCategory(schoolDetail.getSchoolCategory());
        //修改审核信息
        if (!eduAddressSchoolAuditService.updateById(addressSchoolAudit)) {
            throw new AppException("审核状态更新失败,请刷新待审列表后重试");
        }
        //如果审核状态通过，将数据同步到edu_address_tree表中
        if (AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())) {
            EduAddressSchoolAuditVO auditVO = new EduAddressSchoolAuditVO();
            auditVO.setAddressId(addressSchoolAudit.getAddressId());
            auditVO.setSchoolId(addressSchoolAudit.getSchoolId());
            auditVO.setId(addressSchoolAudit.getId());
            auditVO.setNativeFlag(addressSchoolAudit.getNativeFlag());
            auditVO.setServiceAreaFlag(addressSchoolAudit.getServiceAreaFlag());
            eduAddressSchoolService.boundSchoolAndAddress(auditVO);
        }
        //校验地址学区匹配的状态
        eduAddressService.checkAddressMatchingStatus(eduAddress);
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0017", title = "批量取消关联", funcType = FuncTypeEnum.delete)
    @Transactional
    public void batchDelete(EduAddressSchoolKeyVO vo) {
        if (CollectionUtils.isEmpty(vo.getAddressSchoolIds())){
            throw new AppException("请选择要取消关联的数据");
        }
        List<String> addressSchoolIds = vo.getAddressSchoolIds();
        for (String addressSchoolId : addressSchoolIds) {
            EduAddressSchoolKeyVO keyVO = new EduAddressSchoolKeyVO();
            keyVO.setAddressSchoolId(addressSchoolId);
            del(keyVO);
        }
    }

    @Override
    @OpApi(funcCode = "eduAddressSchool0018", title = "地址学校关联表批量修改", funcType = FuncTypeEnum.update)
    @Transactional
    public void batchUpdate(EduAddressSchoolKeyVO vo) {
        if (StringUtils.isBlank(vo.getSchoolId())){
            vo.setSchoolId(SessionUtil.getOrgId());
        }
        if(StringUtils.isBlank(vo.getNativeFlag())){
            vo.setNativeFlag(BoolEnum.FALSE.getType());
        }
        if(StringUtils.isBlank(vo.getServiceAreaFlag())){
            vo.setServiceAreaFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getVerificationFlag())) {
            vo.setVerificationFlag(BoolEnum.FALSE.getType());
        }
        if (CollectionUtils.isEmpty(vo.getAddressSchoolIds())){
            throw new AppException("请选择要修改的数据");
        }
        List<String> addressSchoolIds = vo.getAddressSchoolIds();
        for (String addressSchoolId : addressSchoolIds) {
            EduAddressSchool addressSchool = eduAddressSchoolService.getById(addressSchoolId);
            addressSchool.setAddressSchoolId(addressSchoolId);
            addressSchool.setNativeFlag(vo.getNativeFlag());
            addressSchool.setServiceAreaFlag(vo.getServiceAreaFlag());
            addressSchool.setVerificationFlag(vo.getVerificationFlag());
            addressSchool.setSchoolId(vo.getSchoolId());
            EduAddressSchoolVO editVO = Convert.convert(EduAddressSchoolVO.class, addressSchool);
            edit(editVO);
        }

    }


}
